*****************************************************************
* Replication directory for                                   ***
* Prime locations                                             ***
* by Gabriel M. Ahlfeldt, Thilo N.H. Albers, Kristian Behrens ***
* Published in American Economic Review: Insights             ***
*****************************************************************
* 01/2025
* Stata
version 17.0

* This do file compiles weights by disc and US CBSA for use in estimation of employment weights
* Input files were generated in C++ due to computational requirements

* Generate temporary working directory
		capture mkdir $temp
		capture mkdir "$temp/Weights"	
	
* Loop to convert weights files into Stata
	local count = 1
	local USMETROS 12060  12420  14460  16740  16980 17140  17460  18140  19100  19740 19820  26420  26900  28140  29820 30460  31080  33100  33340  33460 34980  35620  36740  37980  38060 38300  38900  39300  40140  40900 41180  41700  41740  41860  41940 42660  45300  47260  47900 
	foreach USmid of numlist `USMETROS' {
		di "...Working on CBSA `USmid', number `count' of `Ncbsafp'..."
		* Read csv data
		qui  import delimited "$data_USMETROS/WEIGHTS/weights/pp_sampling_`USmid'_output.txt", clear 
		* Generate cbsa id
		qui gen cbsafp = `USmid'
		* Convert radians into lat/lon coordinates
		qui gen lon = v1*180/_pi
		qui gen lat = v2*180/_pi
		* Clean data
		qui drop v1 v2
		qui ren v3 employment
			label var employment "Total employment"
		qui ren v4 accounting
			label var accounting "Accounting"
		qui ren v5 centralbank
			label var centralbank "Central bank"
		qui ren v6 consultancy
			label var consultancy "Consultancy"
		qui ren v7 insurance
			label var insurance "Insurance"
		qui ren v8 investmentbank
			label var investmentbank "Investment bank"
		qui ren v9 lawfirm
			label var lawfirm "Law firm"
		qui ren v10 stockexchange
			label var stockexchange "Stock exchange"
		qui ren v11 accounting_global
			label var accounting_global "Accounting (global)"
		qui ren v12 centralbank_global
			label var centralbank_global "Central bank (global)"
		qui ren v13 consultancy_global
			label var consultancy_global "Consultancy (global)"
		qui ren v14 insurance_global
			label var insurance_global "Insurance (global)"
		qui ren v15 investmentbank_global
			label var investmentbank_global "Investment bank (global)"
		qui ren v16 lawfirm_global
			label var lawfirm_global "Law firm (global)"
		qui ren v17 stockexchange_global
			label var stockexchange_global "Stock exchange (global)"
		qui ren v18 tsheadquarter_global
			label var tsheadquarter_global "Headquarters (global)"
		* Save data
		qui save "$temp/Weights/pp_sampling_`USmid'", replace
		local count = `count'+1
	}

* Append data	
	clear
	foreach USmid of numlist `USMETROS' {
		qui append using "$temp/Weights/pp_sampling_`USmid'"
	}
	merge m:1 cbsafp using "$dataoutput/MSA-GlobalCities/metro_pop2015_Overlap_GlobalCity_USMETRO_DATA"
	drop _m
	
* Save data
	save "$temp/US-CBSAs/WeightsEstimation.dta", replace
	
* Script ends